package com.example.resourceSharingPlatform.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.example.resourceSharingPlatform.entity.Transaction;
import com.example.resourceSharingPlatform.mappers.TransactionRowMapper;
import org.springframework.stereotype.Service;

@Service
public class TransactionDao {
	@Autowired
    private JdbcTemplate jdbcTemplate;

	/*
	 * 查询语句
	 * 1.通过订单编号      queryTransactionById(int id)
	 * 2.通过借出者id  queryTransactionByLender_id(String name)
	 * 3.通过借入者id  queryTransactionByBorrow_id(int borrow_id)
	 * 4.通过商品编号       queryTransactionByItem_id(int item_id)
	 */
	
    public Transaction queryTransactionById(int id){
        String sql = "SELECT * FROM transaction WHERE id=? ";
        Transaction transaction = jdbcTemplate.queryForObject(sql, new TransactionRowMapper(), id);
        return  transaction;
    }

    public List<Transaction>  queryTransactionByLender_id(String lender_id){
        String sql = "SELECT * FROM transaction WHERE lender_id=? ";
        List<Transaction> transactions = jdbcTemplate.query(sql, new TransactionRowMapper(), lender_id);
        return  transactions;
    }
    
    public List<Transaction>  queryTransactionByBorrow_id(String borrow_id){
        String sql = "SELECT * FROM transaction WHERE borrow_id=? ";
        List<Transaction> transactions = jdbcTemplate.query(sql, new TransactionRowMapper(), borrow_id);
        return  transactions;
    }
    
    public List<Transaction>  queryTransactionByItem_id(int item_id){
        String sql = "SELECT * FROM transaction WHERE item_id=? ";
        List<Transaction> transactions = jdbcTemplate.query(sql, new TransactionRowMapper(), item_id);
        return  transactions;
    }

//    查询全部订单暂时没用
//    public List<Transaction> queryAll(){
//        List<Transaction> transactionList = new ArrayList<>();
//        String sql = "SELECT * FROM transaction";
//        userList = jdbcTemplate.query(sql, new TransactionRowMapper());
//        return transactionList;
//    }

    /*
     * 插入语句
     * 1.插入订单 insertTransaction(Transaction transaction)
     */
    public Transaction insertTransaction(Transaction transaction){
        String sql = "INSERT INTO transaction VALUES(?,?,?,?,?,?,?,?)";
        updateByTransaction(sql,transaction);
        String sql2 = "SELECT * FROM transaction WHERE id=(SELECT MAX(id) FROM transaction)";
        return jdbcTemplate.queryForObject(sql2,new TransactionRowMapper());
    }
    
    /*
     * 删除语句
     * 1.根据订单id删除订单   deleteTransactionById(int id)
     */

    public boolean deleteTransactionById(int id){
        String sql = "DELETE FROM transaction WHERE id=?";
        int row = jdbcTemplate.update(sql,id); //返回影响到的行数
        return row!= 0;
    }

    /*
     * 更新语句
     * 1.根据订单编号更新，目前只需要更新交易状态 
     *    0代表申请，1代表交易进行中，2代表交易完成
     */
    public boolean updateTransaction(Transaction transaction){
        String sql = "UPDATE transaction SET id=?, time=?, lender_id=?, borrow_id=?, rentalTime=?, state=?,"
        		+ "amount=?,item_id=?"
        		+ " WHERE id="+transaction.getId();
        int row = updateByTransaction(sql, transaction);
        return row != 0;
    }

    private int updateByTransaction(String sql, Transaction transaction){
        return jdbcTemplate.update(sql,
        		                   transaction.getId(),
        		                   transaction.getTime(),
        		                   transaction.getLender_id(),
        		                   transaction.getBorrow_id(),
        		                   transaction.getRentalTime(),
        		                   transaction.getState(),
        		                   transaction.getAmount(),
        		                   transaction.getItem_id()
        		                   );
    }
	

}
